package com.lty.springmvc.crud.handler;

import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.lty.springmvc.crud.dao.DepartmentDao;
import com.lty.springmvc.crud.dao.EmployeeDao;
import com.lty.springmvc.crud.entity.Employee;

@Controller
public class EmployHandler {
	@Autowired
	private EmployeeDao employeeDao;
	
	@Autowired
	private DepartmentDao departmentDao;
	
	/***
	 * 
	 * 显示所有员工信息
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping("/emps")
	public String list(Map<String,Object> map){
		map.put("emps", employeeDao.getAll());
		return "list";
	}
	
	@RequestMapping(value = "/emp", method = RequestMethod.GET)
	public String input(Map<String, Object> map){
		map.put("departments", departmentDao.getDepartments());
		//一个空对象，满足SpringMVC表单回显约定要求！！！
		map.put("employee", new Employee());
		return "input";
	}
	
	@RequestMapping(value = "/emp", method = RequestMethod.POST)
	public String save(@Valid Employee employee,BindingResult result,Map<String,Object> map){
		if(result.getErrorCount() > 0){
			System.out.println("出错啦...");
			for(FieldError error : result.getFieldErrors()){
				System.out.println(error.getField() + ":" + error.getDefaultMessage());
			}
			
			//为回显做准备，部门数据这里需要加，其它不用
			map.put("departments", departmentDao.getDepartments());
			//若验证出错将跳转到出错页面
			return "input";
		}
		
		System.out.println("save: " + employee);
		employeeDao.save(employee);
		return "redirect:emps";
	}
	
	@RequestMapping(value="/emp/{id}",method = RequestMethod.DELETE)
	public String delete(@PathVariable("id") Integer id){
		employeeDao.delete(id);
		//这里不加/会报错？？？
		return "redirect:/emps";
	}
	
	/***
	 * 注意：本方法本可以与上面的input方法合二为一的，但因本方法需要绑定路参，所以需要另写目标方法
	 * 
	 * @param id
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/emp/{id}",method = RequestMethod.GET)
	public String input(@PathVariable("id") Integer id, Map<String,Object> map){
		map.put("employee", employeeDao.get(id));
		map.put("departments", departmentDao.getDepartments());
		return "input";
	}
	
	@RequestMapping(value = "/emp",method = RequestMethod.PUT)
	public String update(Employee employee){
		employeeDao.save(employee);
		return "redirect:/emps";
	}
	
	//my solution
//	@ModelAttribute
//	public void getEmployee(Employee employee,Map<String, Object> map){
//		Integer id = employee.getId();
//		if(id != null){
//			map.put("employee", employeeDao.get(id));
//		}
//	}
	
	/***
	 * 为保持不能修改的属性
	 * @param id
	 * @param map
	 */
	//Author's solution
	@ModelAttribute
	public void getEmployee(@RequestParam(value="id",required=false) Integer id,Map<String, Object> map){
		if(id != null){
			map.put("employee", employeeDao.get(id));
		}
	}
	
	/***
	 * 
	 * 适用场景：User对象有角色集合，页面是角色的id的checkbox，这个时候会用到(避免手动去赋值)
	 * 
	 * @param webDataBinder
	 */
//	@InitBinder
//	public void initBinder(WebDataBinder webDataBinder){
//		webDataBinder.setDisallowedFields("lastName");
//	}
}
